package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1MapCheck;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class MapCheckTable {
  private static final Log _log = LogFactory.getLog(MapTileTable.class);
  
  private static final List<L1MapCheck> _list = new ArrayList<>();
  
  public static MapCheckTable get() {
    return Holder.instance;
  }
  
  private MapCheckTable() {
    load();
  }
  
  private void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `mapids_check`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        L1MapCheck mapCheck = new L1MapCheck();
        mapCheck.setMapid(rs.getInt("mapid"));
        mapCheck.setStartX(rs.getInt("startX"));
        mapCheck.setEndX(rs.getInt("endX"));
        mapCheck.setStartY(rs.getInt("startY"));
        mapCheck.setEndY(rs.getInt("endY"));
        _list.add(mapCheck);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public List<L1MapCheck> getList() {
    return _list;
  }
  
  private static class Holder {
    static MapCheckTable instance = new MapCheckTable();
  }
}
